package com.example.controller;

import com.example.Utils.CommonStringUtils;
import com.example.Utils.DateUtils;
import com.example.models.Education;
import com.example.models.User;
import com.example.struts.HttpParams;
import io.ebean.Ebean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class WebViewController {
    @RequestMapping(value = "/app/edit_or_show_userinfo/", method = RequestMethod.GET)
    public ModelAndView editOrShowUserInfo(HttpServletRequest request) {
        HttpParams params = new HttpParams(request);
        Integer userId = params.getIntParam("user_id", 0);
        User user = User.find.byId(userId);
        ModelAndView modelAndView;
        if (user == null) {
            modelAndView = new ModelAndView("error/open_failure");
            return modelAndView;
        } else {
            modelAndView = new ModelAndView("user_info/user_info.html");
            modelAndView.addObject("user", user);
            return modelAndView;
        }
    }

    @RequestMapping(value = "/app/submit_modify_userinfo/", method = RequestMethod.POST)
    @ResponseBody
    public String submitModifyUserInfo(HttpServletRequest request) {
        HttpParams params = new HttpParams(request);
        String msg = "error";
        params.printParameter();
        Integer userId = params.getIntParam("user_id", 0);
        User user = User.find.byId(userId);
        String username = params.getParam("username");
        String homeTown = params.getParam("home_town");
        String birthday = params.getParam("birthday");
        String schoolName = params.getParam("school_name");
        String mobile = params.getParam("mobile");
        String brief = params.getParam("brief");
        if(user == null || CommonStringUtils.isNullOrEmpty(username) || CommonStringUtils.isNullOrEmpty(schoolName) || CommonStringUtils.isNullOrEmpty(mobile)){
            return msg;
        }
        Integer repeat = User.search().ne("id",userId).eq("username",username).findCount();
        if(repeat > 0){
            return "repeat";
        }
        Ebean.beginTransaction();
        try{
            String[] pca = homeTown.split(" ");
            Integer len = pca.length;
            if (len > 0) {
                user.setProvince(pca[0]);
            }
            if (len > 1) {
                user.setCity(pca[1]);
            }
            if (len > 2) {
                String temp = "";
                for (int i = 2; i < len; i++) {
                    temp += pca[i] + " ";
                }
                user.setArea(temp.substring(0, temp.length() - 1));
            }
            user.setMobile(mobile);
            user.setUsername(username);
            user.setBirthday(DateUtils.StrToDate(birthday,"yyyy-MM-dd"));
            Education education = Education.search().eq("eduName",schoolName).findOne();
            user.setSchool(education);
            user.setBrief(brief);
            user.setLastModifyTime(new Date());
            user.save();
            Ebean.commitTransaction();
            msg = "success";
        }catch (Exception e){
            Ebean.rollbackTransaction();
        }finally {
            Ebean.endTransaction();
        }
        return msg;
    }
}
